﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMSTOCK : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMSTOCK()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = " update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='I_TS_STOCK'";
            StaticClass.myDB.doexec(sql);

            myCache.doCache();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select b.fpoint_name,a.* "
             +" from i_ts_stock a,a_ts_point b"
             +" where a.fparent_no = b.fpoint_no";
            DataRowView dr1 = (DataRowView)cbfparent_no.SelectedItem;
            if ((dr1 != null) && (cbfparent_no.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fparent_no='" + dr1.Row["fpoint_no"].ToString()+"'";
            }
            DataRowView dr2 = (DataRowView)cbfpoint_state.SelectedItem;
            if ((dr2 != null) && (cbfpoint_state.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fpoint_state=" + dr2.Row["fDictNo"].ToString();
            }
            if (qifstockno.Text != "")
            {
                SQL = SQL + " and ((fstockno like '%" + qifstockno.Text + "%')"
                 + " or (fstockname like '%" + qifstockno.Text + "%'))";
            }
            SQL = SQL + "  order by fpoint_no";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMSTOCK_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_stock";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("A_TS_POINT", "fpoint_flag=2");
            uifparent_no.DataSource = myTs_Dict;
            uifparent_no.DisplayMember = "fpoint_name";
            uifparent_no.ValueMember = "fpoint_no";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fpoint_name"] = "请选择";
            dr1["fpoint_no"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            cbfparent_no.DataSource = myTs_Dict;
            cbfparent_no.DisplayMember = "fpoint_name";
            cbfparent_no.ValueMember = "fpoint_no";
            cbfparent_no.SelectedIndex = 0;

            DataTable myTs_State = myCache.doGetDict("S_TS_DICT", "fDictType=1000");
            uifpoint_state.DataSource = myTs_State;
            uifpoint_state.DisplayMember = "fDictName";
            uifpoint_state.ValueMember = "fDictNo";

            DataRow dr2 = myTs_State.NewRow();
            dr2["fDictName"] = "请选择";
            dr2["fDictNo"] = "0";
            myTs_State.Rows.InsertAt(dr2, 0);
            cbfpoint_state.DataSource = myTs_State;
            cbfpoint_state.DisplayMember = "fDictName";
            cbfpoint_state.ValueMember = "fDictNo";
            cbfpoint_state.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifstockno.Text = "";
            uifstockname.Text = "";
            uifmemo.Text = "";
            uifvideo_url.Text = "";
            uifperson.Text = "";
            uifparent_no.SelectedValue = "";
            uifpoint_state.SelectedIndex = 1;

            sType = "A";
            doLoadData();
            uifstockno.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifstockno.Text = uiDGView["fstockno", rowindex].Value.ToString();
                uifstockname.Text = uiDGView["fstockname", rowindex].Value.ToString();
                uifmemo.Text = uiDGView["fmemo", rowindex].Value.ToString();
                uifvideo_url.Text = uiDGView["fvideo_url", rowindex].Value.ToString();
                uifperson.Text = uiDGView["fperson", rowindex].Value.ToString();

                int i = 0;
                string sstatus = uiDGView["fpoint_state", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifpoint_state.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sstatus))
                    {
                        uifpoint_state.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfpoint_no = uiDGView["fparent_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifparent_no.Items)
                {
                    string id = dr["fpoint_no"].ToString();
                    if (id.Equals(sfpoint_no))
                    {
                        uifparent_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifstockno.ReadOnly = true;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fstockname", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fstockno", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'i_ts_stock','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifstockno, "请输入仓库编号")
                   && CheckEmpty(uifstockname, "请输入仓库名称")
                   && CheckEmpty(uifparent_no, "请选择存放地点")
                   && CheckEmpty(uifpoint_state, "请选择仓库状态");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifpoint_state.SelectedItem;
            string sstatus = drv1.Row["fDictNo"].ToString();

            DataRowView drv2 = (DataRowView)uifparent_no.SelectedItem;
            string sfpoint_no = drv2.Row["fpoint_no"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        i_ts_stock myData = context.i_ts_stock.Find(uifstockno.Text);
                        if (myData is null)
                        {
                            i_ts_stock myData2 = new i_ts_stock();
                            myData2.fstockno = uifstockno.Text;
                            myData2.fstockname = uifstockname.Text;
                            myData2.fmemo = uifmemo.Text;
                            myData2.fvideo_url = uifvideo_url.Text;
                            myData2.fperson = uifperson.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fsite_no = sSite_No;
                            myData2.fpoint_state = int.Parse(sstatus);
                            myData2.fparent_no = sfpoint_no;
                            context.i_ts_stock.Add(myData2);
                            context.SaveChanges();
                            doProc();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    i_ts_stock myts_stock = context.i_ts_stock.Find(uifstockno.Text);

                    myts_stock.fstockname = uifstockname.Text;
                    myts_stock.fmemo = uifmemo.Text;
                    myts_stock.fvideo_url = uifvideo_url.Text;
                    myts_stock.fperson = uifperson.Text;
                    myts_stock.foperator = sUser;
                    myts_stock.fupddate = DateTime.Now;
                    myts_stock.fpoint_state = int.Parse(sstatus);
                    myts_stock.fparent_no = sfpoint_no;
                    int result = context.SaveChanges();
                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void cbfparent_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbfpoint_state_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
